package com.module.shici.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.module.shici.R;
import com.module.shici.sql.ShiCi;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

public class BannerImgAdapter extends BannerAdapter<ShiCi, BannerImgAdapter.BannerViewHolder> {

    public BannerImgAdapter(List<ShiCi> mDatas) {
        //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
        super(mDatas);
        this.mDatas = mDatas;
    }

    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.module_shici_rv_banner, parent, false);
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        itemView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        BannerViewHolder viewHolder = new BannerViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindView(BannerViewHolder holder, ShiCi data, int position, int size) {
        //将，或、替换为 ·
        String title = data.getTitle().replaceAll("，|/","·");
        holder.title.setText(title);
        holder.author.setText(data.getChaodai()+"·"+data.getAuthor());
        holder.bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bannerListener != null){
                    System.out.println("诗词"+data);
                    bannerListener.onItemClick(data.getTitle(),data.getAuthor(),data.getContent(),data.getFanyi(),data.getZhushi(),data.getChaodai());
                }
            }
        });
    }

    public interface BannerListener{
        void onItemClick(String title, String author, String content, String fanyi, String zhushi, String chaodai);
    }

    private BannerListener bannerListener;

    public void getBannerOnItemClick(BannerListener bannerListener){
        this.bannerListener = bannerListener;
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {

        private TextView title,author;
        private ImageView bg;
        public BannerViewHolder(@NonNull View view) {
            super(view);
            author = view.findViewById(R.id.rv_banner_author);
            title = view.findViewById(R.id.rv_banner_title);
            bg = view.findViewById(R.id.rv_banner_bg);
        }
    }
}
